package cc.telecomdigital.tdstock.Http.Tools;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

import org.ksoap2.serialization.SoapObject;

import cc.telecomdigital.tdstock.Framework.UniqueItem.TDStockLog;

public class H_ReflectionUtils
{
	static final private String	LOG_TAG	= "H_ReflectionUtils";

	static public Object GetInstanceToField(/*String entityName*/Class<?> cls, String... args)
	{
		try
		{
//			Class<?> cls = Class.forName(entityName);
			
			Object obj = cls.newInstance();
			
			Field[] fields = cls.getDeclaredFields();

			for (Field field : fields)
			{
				if (!Modifier.isPublic(field.getModifiers()))
				{
					field.setAccessible(true);
				}

				for (int j = 0; j < args.length; j++)
				{
					String str = args[j];
					String strs[] = str.split(",");
					if (strs[0].equals(field.getName()))
					{
						field.set(obj, strs[1]);
						break;
					}
				}
			}
			return obj;
		}
		catch (IllegalAccessException e)
		{
			TDStockLog.e(LOG_TAG, "IllegalAccessException>>" + e.toString());
			e.printStackTrace();
		}
		/*catch (ClassNotFoundException e)
		{
			TDStockLog.e(LOG_TAG, "ClassNotFoundException>>" + e.toString());
			e.printStackTrace();
		}*/
		catch (InstantiationException e)
		{
			TDStockLog.e(LOG_TAG, "InstantiationException>>" + e.toString());
			e.printStackTrace();
		}
		return null;
	}

	static public Object GetInstanceToSetMethod(/*String entityName*/Class<?> cls, SoapObject soapObject)
	{
		try
		{
//			Class<?> cls = Class.forName(entityName);
			
			Object obj = cls.newInstance();
			
			Field[] fields = cls.getDeclaredFields();

			for (Field fd : fields)
			{
				String fieldName = fd.getName(); // 字段名称

				// 根据字段名称，得到set方法名称
				String stringLetter = fieldName.substring(0, 1).toUpperCase();
				String setName = "set" + stringLetter + fieldName.substring(1);
				
				Method setMethod = cls.getMethod(setName, new Class[] { fd.getType() });

				// 设置非共有类属性权限
				if (!Modifier.isPublic(fd.getModifiers()))
				{
					fd.setAccessible(true);
				}
				// 调用set方法赋值
				Object o = "";
				try
				{
					if (fieldName != null && fieldName.length() > 1 && fieldName.startsWith("_"))
						fieldName = fieldName.substring(1);
					o = soapObject.getProperty(fieldName);
				}
				catch (Exception e)
				{
				}

				setMethod.invoke(obj, o.toString());
			}
			return obj;
		}
		catch (IllegalArgumentException e)
		{
			TDStockLog.e(LOG_TAG, "IllegalArgumentException>>" + e.toString());
			e.printStackTrace();
		}
		catch (IllegalAccessException e)
		{
			TDStockLog.e(LOG_TAG, "IllegalAccessException>>" + e.toString());
			e.printStackTrace();
		}
		catch (InvocationTargetException e)
		{
			TDStockLog.e(LOG_TAG, "InvocationTargetException>>" + e.toString());
			e.printStackTrace();
		}
		/*catch (ClassNotFoundException e)
		{
			TDStockLog.e(LOG_TAG, "ClassNotFoundException>>" + e.toString());
			e.printStackTrace();
		}*/
		catch (SecurityException e)
		{
			TDStockLog.e(LOG_TAG, "SecurityException>>" + e.toString());
			e.printStackTrace();
		}
		catch (NoSuchMethodException e)
		{
			TDStockLog.e(LOG_TAG, "NoSuchMethodException>>" + e.toString());
			e.printStackTrace();
		}
		catch (InstantiationException e)
		{
			TDStockLog.e(LOG_TAG, "InstantiationException>>" + e.toString());
			e.printStackTrace();
		}
		catch (Exception e)
		{
			TDStockLog.e(LOG_TAG, "Exception>>" + e.toString());
			e.printStackTrace();
		}
		return null;
	}
}
